import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.sql.Date;
import java.util.List;



/**
 * Connects to the server.
 * 
 * @author Joshua Harris
 * @version 1.0
 */
public class Client {
	
    /** the port to run on */
    private int port;

    private Socket sock;
    
	private BufferedReader  inStream;
    
	private PrintWriter outStream;
    
    /**
     * Constructor for the class
     *
     * @param port  the port to run on
     */
    public Client(final int port) {
        this.port = port;
    }

    /**
     * This method initializes a socket to communicate with the server.
     * 
     * @return the socket that is initialized.
     */
    private void initialize() {
        try {
            InetAddress ipa = InetAddress.getLocalHost();
        	sock = new Socket(ipa, port);
        	inStream = new BufferedReader(new InputStreamReader(
                    sock.getInputStream()));
        	outStream = new PrintWriter(sock.getOutputStream());
        	outStream.flush();
        } catch (UnknownHostException e){
        	System.err.println("HOST EXCEPTION: client.java - initialize()");
        } catch (IOException e) {
        	System.err.println("SOCKET INITIALIZATION ERROR");
        }
    }
    
    private void sendMessage(String message){
    	initialize();
    	outStream.write(message);
    	outStream.flush();
    }
    
    private String sendAndRecieveMessage(String message){
    	sendMessage(message);
    	try{
    		while(!inStream.ready());
    		return inStream.readLine();
    	}catch(Exception e){
    		return null;
    	}
    }
    
    
    public User login(String username, String password, int role){
    	String message = sendAndRecieveMessage(
    			MessageEditor.makeLogin(username, password, role));
    	return MessageEditor.parseRLogin(message);
    }
    
    public void addUser(User user){
    	sendMessage(MessageEditor.makeAddUser(user));
    }
    
    public User loadUser(int ssn){
    	String message = sendAndRecieveMessage(
    			MessageEditor.makeLoadUser(ssn));
    	return MessageEditor.parseRLoadUser(message);
    }
    
    public void updateUser(User user){
    	sendMessage(MessageEditor.makeUpdateUser(user.getSSN(), user));
    }
    
    public void deleteUser(int ssn){
    	sendMessage(MessageEditor.makeDeleteUser(ssn));
    }
    
    public List<User> searchUser(String name){
    	String message = sendAndRecieveMessage(
    			MessageEditor.makeSearchUser(name));
    	return MessageEditor.parseRSearchUser(message);
    }
    
    public void addApp(Appointment app){
    	sendMessage(MessageEditor.makeAddApp(app));
    }

    public void updateApp(Appointment app){
    	sendMessage(MessageEditor.makeUpdateApp(app));
    }
    
    public void deleteApp(Appointment app){
    	sendMessage(MessageEditor.makeDeleteApp(app.getSSN(), app.getDate()));
    }
    
    public List<Appointment> searchAppSSN(int ssn){
    	String message = sendAndRecieveMessage(
    			MessageEditor.makeSearchAppSSN(ssn));
    	return MessageEditor.parseRSearchAppSSN(message);
    }
    
    public List<Appointment> searchAppDate(Date date){
    	String message = sendAndRecieveMessage(
    			MessageEditor.makeSearchAppDate(date));
    	return MessageEditor.parseRSearchAppDate(message);
    }
    
    public void quit(){
    	sendMessage(MessageEditor.makeQuit());
    }
    
    public static void main(String[] args){
    	Client client = new Client(45678);
    	client.quit();
    	
    }
}